package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.api.user.UserClientApi;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.pay.dto.OutDto;
import com.zhentao.model.pay.dto.PayDto;
import com.zhentao.model.pay.pojo.ApUserFinancialDetails;
import com.zhentao.model.pay.vo.BalanceVo;
import com.zhentao.model.pay.vo.FinancialVo;
import com.zhentao.model.pay.vo.PayVo;
import com.zhentao.model.user.pojo.ApUser;
import com.zhentao.model.user.pojo.ApUserInfo;
import com.zhentao.service.ApUserFinancialDetailsService;
import com.zhentao.mapper.ApUserFinancialDetailsMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 许文倩
* @description 针对表【ap_user_financial_details】的数据库操作Service实现
* @createDate 2024-07-12 10:14:25
*/
@Service
public class ApUserFinancialDetailsServiceImpl extends ServiceImpl<ApUserFinancialDetailsMapper, ApUserFinancialDetails>
    implements ApUserFinancialDetailsService{
    @Resource
    ApUserFinancialDetailsMapper financialDetailsMapper;
    @Resource
    UserClientApi userClientApi;

    @Override
    public PageInfo<FinancialVo> getList(PayDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<FinancialVo> payVos=financialDetailsMapper.financialList(dto.getNickName());
        PageInfo<FinancialVo> financialVoPageInfo=new PageInfo<>(payVos);
        return financialVoPageInfo;
    }


    @Override
    public PageInfo<FinancialVo> notCheckList(PayDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<FinancialVo> payVos=financialDetailsMapper.notCheckfinancialList(dto.getNickName());
        PageInfo<FinancialVo> financialVoPageInfo=new PageInfo<>(payVos);
        return financialVoPageInfo;
    }

//    @Override
//    public ResponseResult balance(Integer userId) {
//        QueryWrapper<ApUserFinancialDetails> wrapper=new QueryWrapper<>();
//        wrapper.lambda().eq(ApUserFinancialDetails::getUserId,userId);
//        List<ApUserFinancialDetails> apUserFinancialDetails = financialDetailsMapper.selectList(wrapper);
//        BalanceVo balanceVo=new BalanceVo();
//        ApUser apUser = userClientApi.selectOne(userId);
//        double sum=0;
//        for (ApUserFinancialDetails financialDetails:apUserFinancialDetails){
//            sum+=financialDetails.getMoney();
//        }
//        apUser.setBalance(sum);
//
//        return ResponseResult.okResult(apUser);
//    }

    @Override
    public ResponseResult loser(OutDto dto) {
        if (dto==null || dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserFinancialDetails apUserFinancialDetails = getById(dto.getId());
        if (apUserFinancialDetails==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"提现信息不存在");
        }
        apUserFinancialDetails.setAuditStatus(2);
        apUserFinancialDetails.setFeedback(dto.getFeedback());
        financialDetailsMapper.updateById(apUserFinancialDetails);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult status(Integer id) {
        ApUserFinancialDetails apUserFinancialDetails = getById(id);
        if (apUserFinancialDetails==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"提现信息不存在");
        }
        apUserFinancialDetails.setAuditStatus(1);
        financialDetailsMapper.updateById(apUserFinancialDetails);
        ApUser apUser = userClientApi.selectOne(apUserFinancialDetails.getUserId());
        apUser.setBalance(apUser.getBalance()-apUserFinancialDetails.getMoney());
        userClientApi.updateUser(apUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult outList(Integer userId) {
        List<ApUserFinancialDetails> list=financialDetailsMapper.outList(userId);
        return ResponseResult.okResult(list);
    }

    public ApUserFinancialDetails getById(Integer id){
        ApUserFinancialDetails financialDetails = financialDetailsMapper.selectById(id);
        return financialDetails;
    }
}




